/*
  The MIT License
  
  Copyright (c) 2009 smath project
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/
package pl.smath.expression.serializer;

import pl.smath.expression.model.ConjunctionTerm;
import pl.smath.expression.model.DisjunctionTerm;
import pl.smath.expression.model.ExponentiationTerm;
import pl.smath.expression.model.FractionTerm;
import pl.smath.expression.model.AbstractMultiTerm;
import pl.smath.expression.model.InTerm;
import pl.smath.expression.model.NumberSetTerm;
import pl.smath.expression.model.NumberTerm;
import pl.smath.expression.model.ProductTerm;
import pl.smath.expression.model.RelationTerm;
import pl.smath.expression.model.RootTerm;
import pl.smath.expression.model.SetSumTerm;
import pl.smath.expression.model.SetTerm;
import pl.smath.expression.model.SumTerm;
import pl.smath.expression.model.Term;
import pl.smath.expression.model.RelationTerm.RelationType;

public class LatexSerializer extends AbstractSerializer{

	/**
	 * Serializacja \in
	 */
	protected String process(InTerm inputTerm) {
		String output = "";
		
		output += serialize(inputTerm.getElement());
		
		output += inputTerm.getText();

		output += serialize(inputTerm.getSet());
		
		return output;
	}


	protected String process(RelationTerm term) {
		String output;
		
		output = serialize(term.getLeftTerm());
		if(term.getRelationType() == RelationType.EQUALITY){
			output += " = ";
		}
		else if(term.getRelationType() == RelationType.INEQUALITY){
			output += " \\neq ";
		}
		else if(term.getRelationType() == RelationType.GREATER_THEN){
			output += " > ";
		}
		else if(term.getRelationType() == RelationType.GREATER_THEN_OR_EQUAL_TO){
			output += " \\geq ";
		}
		else if(term.getRelationType() == RelationType.LESS_THEN){
			output += " < ";
		}
		else if(term.getRelationType() == RelationType.LESS_THEN_OR_EQUAL_TO){
			output += " \\leq ";
		}

		output += serialize(term.getRightTerm());
		
		return output;
	}


	protected String process(NumberSetTerm term) {
		
		String output;
		
		if(term.isNegative()){
			output = " \\setminus ";
		}
		else{
			output = "";
		}
		
		output += "\\mathbb{" + term.getText() + "}";
		
		return output;
	}


	/**
	 * Serializacja potegi
	 */
	protected String process(ExponentiationTerm expTerm) {
		String output = "";
		
		if(expTerm.isNegative())
			output = "-";
		
		if(expTerm.getBase() instanceof AbstractMultiTerm || expTerm.getBase().isNegative())
			output += "(" + serialize(expTerm.getBase()) + ")";
		else
			output += serialize(expTerm.getBase());
		
		output += "^";

		output += "{" + serialize(expTerm.getExponent()) + "}";
		
		return output;
	}


	/**
	 * serializacja ulamka
	 */
	protected String process(FractionTerm fracTerm) {
		String up;
		String down;
		String output;
		
		up = serialize(fracTerm.getNumerator());
		down = serialize(fracTerm.getDenominator());
		if(fracTerm.getText().compareTo("/") == 0){
			output = "\\frac{"+ up + "}{" + down + "}";
		}
		else{
			if(fracTerm.getNumerator() instanceof AbstractMultiTerm){
				output = "(" + up + ")";
			}
			else{
				output = up;
			}
			
			output += " \\colon ";

			if(fracTerm.getDenominator() instanceof AbstractMultiTerm){
				output += "(" + down + ")";
			}
			else{
				output += down;
			}
		}

		if(fracTerm.isNegative())
			output = "-" + output;
		
		return output;
	}


	/**
	  * Obsługa Number i Symbol Term
	  * @param term
	  * @return
	  */
	 protected String process(Term term){
		 return term.toString();
	 }

	 /**
	  * Serializacja sumy
	  * @param term
	  * @return
	  */
	 protected String process(SetTerm inputTerm) {
		String output = "";
		
		for(Term term : inputTerm.getTerms()){

			if(output.length() > 0){
				output += ", ";
			}

			output += serialize(term);
		}

		output = "\\{" + output + "\\}";
		
		if(inputTerm.isNegative())
			output = " \\setminus " + output;
		
		return output;
	 }


	 /**
	  * Serializacja sumy zbiorów
	  * @param term
	  * @return
	  */
	 protected String process(SetSumTerm inputTerm) {
		String output = "";
		
		
		for(Term term : inputTerm.getTerms()){
			
			if(output.length() > 0){
				if(!term.isNegative()){
					output += " \\cup ";
				}
			}

			output += serialize(term);
		}

		if(inputTerm.isNegative())
			output = "\\setminus (" + output + ")";
		
		return output;
	 }


	 /**
	  * Serializacja sumy
	  * @param term
	  * @return
	  */
	 protected String process(SumTerm inputTerm) {
		String output = "";
		
		
		for(Term term : inputTerm.getTerms()){
			
			if(output.length() > 0){
				if(!term.isNegative()){
					output += "+";
				}
			}

			if(term instanceof SumTerm && !term.isNegative())
				output += "(" + serialize(term) + ")";
			else
				output += serialize(term);
		}

		if(inputTerm.isNegative())
			output = "-(" + output + ")";
		
		return output;
	 }


	 /**
	  * Serializacja alternaty logicznej
	  * @param term
	  * @return
	  */
	 protected String process(DisjunctionTerm inputTerm) {
		
		 String output = "";
			
		 for(Term term : inputTerm.getTerms()){
			
			if(output.length() > 0){
				output += " \\vee ";
			}

			if(term instanceof ConjunctionTerm){
				output += "(" + serialize(term) + ")";
			}
			else{
				output += serialize(term);
			}
		 }
		
		 if(inputTerm.isNegative())
			 output = "-" + output;
		
		return output;
	 }


	 /**
	  * Serializacja alternaty logicznej
	  * @param term
	  * @return
	  */
	 protected String process(ConjunctionTerm inputTerm) {
			String output = "";
			
			for(Term term : inputTerm.getTerms()){
				
				if(output.length() > 0){
					output += " \\wedge ";
				}

				if(term instanceof DisjunctionTerm){
					output += "(" + serialize(term) + ")";
				}
				else{
					output += serialize(term);
				}
			}
			
			if(inputTerm.isNegative())
				output = "-" + output;
			
			return output;
	 }


	 /**
	  * Serializacja iloczynu
	  * @param term
	  * @return
	  */
	 protected String process(ProductTerm productTerm) {
			String output = "";
			boolean lastNeedDot = false;
			
			for(Term term : productTerm.getTerms()){
				
				if(output.length() > 0){
					if( lastNeedDot && productDotNeeded(term) ){
						output += " \\cdot ";
					}
					else{
						output += " ";
					}
				}

				lastNeedDot = productDotNeeded(term);
				
				if(term instanceof AbstractMultiTerm || term.isNegative())
					output += "(" + serialize(term) + ")";
				else
					output += serialize(term);
			}
			
			if(productTerm.isNegative())
				output = "-" + output;
			
			return output;
	 }


    /**
     * Sprawdzenie czy wyrazenie powinno być rozdzielane kropka	 
     * @param term
     * @return
     */
    private boolean productDotNeeded(Term term) {
    	
    	Term checkTerm = term;
    	
    	if(term instanceof ExponentiationTerm){
    		checkTerm = ((ExponentiationTerm) term).getBase();
    	}
    	else if(term instanceof RootTerm){
    		checkTerm = ((RootTerm) term).getBase();
    	}
    	
		return checkTerm instanceof NumberTerm;
	}


	/**
     * Obsługa pierwiastka	 
     */
	protected String process(RootTerm term) {
		String base;
		String exp;
		String output;
		
		exp = serialize(term.getDegree());
		base = serialize(term.getBase());
		if(exp.compareTo("2") == 0)
			output = "\\sqrt{"+ base + "}";
		else
			output = "\\sqrt["+ exp + "]{" + base + "}";

		if(term.isNegative())
			output = "-" + output;
		
		return output;
	}

}       
